﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace FrameworkLib.BLL
{
    public static class BLLExtension
    {



        public static bool WhenNotNull<T>(this T value, Action func) where T : class
        {

            if (value != null)
            {

                func();

                return true;

            }



            return false;

        }



        public static bool WhenNotNull<T>(this Nullable<T> value, Action func) where T : struct
        {

            if (value != null)
            {

                func();

                return true;

            }

            return false;

        }



        public static bool WhenNotNull<T>(this T value, Action<T> func) where T : class
        {

            if (value != null)
            {

                func(value);

                return true;

            }



            return false;

        }



        public static V WhenNotNull<T, V>(this T value, Func<V> func)
            where T : class
            where V : class
        {

            if (value != null)
            {

                return func();

            }



            return null;

        }



        public static V WhenNotNull<T, V>(this Nullable<T> value, Func<V> func)

            where T : struct

            where V : class
        {

            if (value != null)
            {

                return func();

            }



            return null;

        }



        public static V WhenNotNullS<T, V>(this Nullable<T> value, Func<V> func)

            where T : struct

            where V : struct
        {

            if (value != null)
            {

                return func();

            }



            return default(V);

        }



        static public void ForNotNull<T>(this IEnumerable<T> list, Action<T> func) where T : class
        {

            if (list == null)

                return;



            foreach (var v in list)
            {

                if (v != null)

                    func(v);

            }

        }



        static public void ForNotNull<T>(this IEnumerable list, Action<T> func) where T : class
        {

            if (list == null)

                return;



            foreach (var v in list)
            {

                T castedValue = (T)v;

                func(castedValue);

            }

        }




        public static TOut NullSafe<TIn, TOut>(this TIn obj, Func<TIn, TOut> memberAction)
        {
            //Note we should not use obj != null because it can not test value types and also
            //compiler has to lift the type to a nullable type for doing the comparision with null.
            return (EqualityComparer<TIn>.Default.Equals(obj, default(TIn))) ? memberAction(obj) : default(TOut);

        }



    }
}
